package com.botann.projects.controller;

import com.botann.projects.base.Model;
import com.botann.projects.constants.API;
import com.botann.projects.constants.Constant;
import com.botann.projects.model.Role;
import com.botann.projects.base.BaseController;
import com.botann.projects.base.Model;
import com.botann.projects.constants.API;
import com.botann.projects.constants.Constant;
import com.botann.projects.model.Role;
import com.botann.projects.service.RoleServiceIntf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Charging
 * RoleController
 * Created by 刘山国 on 2017/11/29.
 * Copyright (c) 2017 刘山国. All rights reserved
 */
@RestController
public class RoleController extends BaseController {
    @Override
    public Class loggerClass() {
        return RoleController.class;
    }

    @Autowired
    private RoleServiceIntf service;

    /*
     * 是否有同名角色, 用户所属公司内
     * @param roleName String
     * @return  Model
     */
    @RequestMapping(value = API.URL_HAS_SAME_NAME_ROLE, method = RequestMethod.POST)
    public Model hasSameNameRole(@RequestParam String roleName) {
        try {
            Role role = service.loadRoleWithName(roleName);
            if (role != null) {
                return successModel("已存在同名角色!",true);
            } else {
                return successModel(false);
            }
        } catch (Exception e) {
            return failureModel(e);
        }
    }

    /*
     * 添加角色
     * @param roleJson String
     * @return Model
     */
    @RequestMapping(value = API.URL_ADD_ROLE, method = RequestMethod.POST)
    public Model addRole(@RequestParam String roleJson) {
        try {
            return successModel(Constant.ADD_SUCCESS,service.addRole(roleJson));
        } catch (Exception e) {
            return failureModel(e);
        }
    }

    /*
     * 更新角色
     * @param roleJson String
     * @return Model
     */
    @RequestMapping(value = API.URL_UPDATE_ROLE, method = RequestMethod.POST)
    public Model updateRole(@RequestParam String roleJson) {
        try {
            service.updateRole(roleJson);
            return successModel(Constant.UPDATE_SUCCESS,Constant.EMPTY);
        } catch (Exception e) {
            return failureModel(e);
        }
    }


    /*
     * 批量删除角色接口,单个删除也调用这个
     * @param deleteIdsJson   String
     * @return  Model
     */
    @RequestMapping(value = API.URL_DELETE_ROLES)
    public Model deleteRole(@RequestParam String deleteIdsJson) {
        try {
            service.deleteRoles(jsonToListModel(deleteIdsJson,Integer.class));
            return successModel(Constant.DELETE_SUCCESS,Constant.EMPTY);
        } catch (Exception e) {
            return failureModel(e);
        }
    }

    /*
     * 角色列表
     * @param companyId Integer
     * @return Model
     */
    @RequestMapping(value = API.URL_ROLES, method = RequestMethod.POST)
    public Model roles() {
        try {
            return successModel(service.loadRoles());
        } catch (Exception e) {
            return failureModel(e);
        }
    }




}
